{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Minimum Cost to Set Cooking Time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: minCostSetTime"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #设置时间的最少代价"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>常见的微波炉可以设置加热时间，且加热时间满足以下条件：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>至少为 <code>1</code>&nbsp;秒钟。</li>\n",
    "\t<li>至多为&nbsp;<code>99</code>&nbsp;分&nbsp;<code>99</code>&nbsp;秒。</li>\n",
    "</ul>\n",
    "\n",
    "<p>你可以 <strong>最多</strong>&nbsp;输入&nbsp;<strong>4 个数字</strong>&nbsp;来设置加热时间。如果你输入的位数不足 4 位，微波炉会自动加 <strong>前缀</strong>&nbsp;<strong>0</strong>&nbsp;来补足 4 位。微波炉会将设置好的四位数中，<strong>前</strong>&nbsp;两位当作分钟数，<strong>后</strong>&nbsp;两位当作秒数。它们所表示的总时间就是加热时间。比方说：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>你输入&nbsp;<code>9</code> <code>5</code> <code>4</code>&nbsp;（三个数字），被自动补足为&nbsp;<code>0954</code>&nbsp;，并表示&nbsp;<code>9</code>&nbsp;分&nbsp;<code>54</code>&nbsp;秒。</li>\n",
    "\t<li>你输入&nbsp;<code>0</code> <code>0</code> <code>0</code> <code>8</code>&nbsp;（四个数字），表示&nbsp;<code>0</code>&nbsp;分&nbsp;<code>8</code>&nbsp;秒。</li>\n",
    "\t<li>你输入&nbsp;<code>8</code> <code>0</code> <code>9</code> <code>0</code>&nbsp;，表示&nbsp;<code>80</code>&nbsp;分&nbsp;<code>90</code>&nbsp;秒。</li>\n",
    "\t<li>你输入&nbsp;<code>8</code> <code>1</code> <code>3</code> <code>0</code>&nbsp;，表示&nbsp;<code>81</code>&nbsp;分&nbsp;<code>30</code>&nbsp;秒。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你整数&nbsp;<code>startAt</code>&nbsp;，<code>moveCost</code>&nbsp;，<code>pushCost</code>&nbsp;和&nbsp;<code>targetSeconds</code>&nbsp;。<strong>一开始</strong>，你的手指在数字&nbsp;<code>startAt</code>&nbsp;处。将手指移到<strong>&nbsp;任何其他数字</strong>&nbsp;，需要花费&nbsp;<code>moveCost</code>&nbsp;的单位代价。<strong>每</strong>&nbsp;输入你手指所在位置的数字一次，需要花费&nbsp;<code>pushCost</code>&nbsp;的单位代价。</p>\n",
    "\n",
    "<p>要设置&nbsp;<code>targetSeconds</code>&nbsp;秒的加热时间，可能会有多种设置方法。你想要知道这些方法中，总代价最小为多少。</p>\n",
    "\n",
    "<p>请你能返回设置&nbsp;<code>targetSeconds</code>&nbsp;秒钟加热时间需要花费的最少代价。</p>\n",
    "\n",
    "<p>请记住，虽然微波炉的秒数最多可以设置到 <code>99</code>&nbsp;秒，但一分钟等于&nbsp;<code>60</code>&nbsp;秒。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/30/1.png\" style=\"width: 506px; height: 210px;\"></p>\n",
    "\n",
    "<pre><b>输入：</b>startAt = 1, moveCost = 2, pushCost = 1, targetSeconds = 600\n",
    "<b>输出：</b>6\n",
    "<b>解释：</b>以下为设置加热时间的所有方法。\n",
    "- 1 0 0 0 ，表示 10 分 0 秒。\n",
    "&nbsp; 手指一开始就在数字 1 处，输入 1 （代价为 1），移到 0 处（代价为 2），输入 0（代价为 1），输入 0（代价为 1），输入 0（代价为 1）。\n",
    "&nbsp; 总代价为：1 + 2 + 1 + 1 + 1 = 6 。这是所有方案中的最小代价。\n",
    "- 0 9 6 0，表示 9 分 60 秒。它也表示 600 秒。\n",
    "&nbsp; 手指移到 0 处（代价为 2），输入 0 （代价为 1），移到 9 处（代价为 2），输入 9（代价为 1），移到 6 处（代价为 2），输入 6（代价为 1），移到 0 处（代价为 2），输入 0（代价为 1）。\n",
    "&nbsp; 总代价为：2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 = 12 。\n",
    "- 9 6 0，微波炉自动补全为 0960 ，表示 9 分 60 秒。\n",
    "&nbsp; 手指移到 9 处（代价为 2），输入 9 （代价为 1），移到 6 处（代价为 2），输入 6（代价为 1），移到 0 处（代价为 2），输入 0（代价为 1）。\n",
    "&nbsp; 总代价为：2 + 1 + 2 + 1 + 2 + 1 = 9 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/30/2.png\" style=\"width: 505px; height: 73px;\"></p>\n",
    "\n",
    "<pre><b>输入：</b>startAt = 0, moveCost = 1, pushCost = 2, targetSeconds = 76\n",
    "<b>输出：</b>6\n",
    "<b>解释：</b>最优方案为输入两个数字 7 6，表示 76 秒。\n",
    "手指移到 7 处（代价为 1），输入 7 （代价为 2），移到 6 处（代价为 1），输入 6（代价为 2）。总代价为：1 + 2 + 1 + 2 = 6\n",
    "其他可行方案为 0076 ，076 ，0116 和 116 ，但是它们的代价都比 6 大。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= startAt &lt;= 9</code></li>\n",
    "\t<li><code>1 &lt;= moveCost, pushCost &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>1 &lt;= targetSeconds &lt;= 6039</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [minimum-cost-to-set-cooking-time](https://leetcode.cn/problems/minimum-cost-to-set-cooking-time/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [minimum-cost-to-set-cooking-time](https://leetcode.cn/problems/minimum-cost-to-set-cooking-time/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1\\n2\\n1\\n600', '0\\n1\\n2\\n76']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        #target的表达最多有两种，一种为最大化分钟，如果对60取余数后小于等于39，则存在第二种表达，最大化秒钟\n",
    "        #ta1 = (targetSeconds // 60) * 100 + targetSeconds % 60\n",
    "        #ta2 = ((targetSeconds // 60)-1) * 100 + targetSeconds % 60 +60\n",
    "        def cost(m : int,s : int) -> int:\n",
    "            if not(0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float(\"INF\")\n",
    "            digits =[m // 10,m % 10,s // 10,s % 10]\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] ==0:\n",
    "                start += 1\n",
    "            ans = 0\n",
    "            prev = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d !=prev:\n",
    "                    prev = d\n",
    "                    ans += moveCost\n",
    "                ans +=pushCost\n",
    "            return ans\n",
    "        mm,ss = targetSeconds // 60,targetSeconds % 60\n",
    "        return min(cost(mm,ss),cost(mm-1,ss+60))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def cost(m: int, s: int) -> int:\n",
    "            if not (0 <= m <= 99 and 0 <= s <= 99):\n",
    "                # 输入不合法\n",
    "                return float(\"INF\")\n",
    "            digits = [m // 10, m % 10, s // 10, s % 10]\n",
    "            # 寻找起始位\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] == 0:\n",
    "                start += 1\n",
    "            \n",
    "            res = 0   # 最小花费\n",
    "            prev = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d != prev:\n",
    "                    # 此时需要先移动再输入\n",
    "                    prev = d\n",
    "                    res += moveCost\n",
    "                res += pushCost\n",
    "            return res\n",
    "        \n",
    "        mm, ss = targetSeconds // 60, targetSeconds % 60\n",
    "        return min(cost(mm, ss), cost(mm - 1, ss + 60))\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "\n",
    "        def cost(m: int, s: int) -> int:\n",
    "            if not (0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float(\"INF\")\n",
    "            digits = [m // 10, m % 10, s // 10, s % 10]\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] == 0:\n",
    "                start += 1\n",
    "\n",
    "            res = 0\n",
    "            prev = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d != prev:\n",
    "                    prev = d\n",
    "                    res += moveCost\n",
    "                res += pushCost\n",
    "            return res\n",
    "\n",
    "        mm, ss = targetSeconds // 60, targetSeconds % 60\n",
    "        return min(cost(mm, ss), cost(mm - 1, ss + 60))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\r\n",
    "        mn = targetSeconds // 60 \r\n",
    "        se = targetSeconds % 60 \r\n",
    "\r\n",
    "        def calc(x, y) -> int:\r\n",
    "            res = 0 \r\n",
    "            if x < 0 or x > 99 or y > 99:\r\n",
    "                return inf \r\n",
    "            cur = startAt\r\n",
    "            is_num = False \r\n",
    "            num = x * 100 + y \r\n",
    "            p = 1000\r\n",
    "            for i in range(4):\r\n",
    "                n = (num // p) % 10\r\n",
    "                p //= 10\r\n",
    "                if n == 0 and not is_num:\r\n",
    "                    continue\r\n",
    "                if cur != n:\r\n",
    "                    res += moveCost \r\n",
    "                res += pushCost\r\n",
    "                cur = n \r\n",
    "                is_num = True \r\n",
    "            return res \r\n",
    "        return min(calc(mn, se), calc(mn - 1, se + 60))\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        ans = inf\n",
    "        for a in range(100):\n",
    "            if a*60 > targetSeconds:\n",
    "                break\n",
    "            for b in range(100):\n",
    "                cur = a*60+b\n",
    "                if cur > targetSeconds:\n",
    "                    break\n",
    "                if cur != targetSeconds:\n",
    "                    continue\n",
    "                aa = str(a)\n",
    "                if len(aa)<2:\n",
    "                    aa =(2-len(aa))*\"0\" + aa\n",
    "\n",
    "                bb = str(b)\n",
    "                if len(bb) < 2:\n",
    "                    bb = (2 - len(bb)) * \"0\" + bb\n",
    "                cost = 0\n",
    "                lst = [int(w) for w in aa+bb]\n",
    "                while lst and lst[0]==0:\n",
    "                    lst.pop(0)\n",
    "                pre = startAt\n",
    "                for num in lst:\n",
    "                    cost += moveCost*int(pre != num)\n",
    "                    cost += pushCost\n",
    "                    pre= num\n",
    "                if cost < ans:\n",
    "                    ans = cost\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def resolve(n1, n2):\n",
    "            if n1 < 0 or n1 > 99 or n2 > 99: return float('inf')\n",
    "            s = str(n2) if n1 == 0 else (str(n1) + (str(n2) if n2 > 9 else '0' + str(n2)))\n",
    "            print(n1, n2,'-> ',s)\n",
    "            res, cur = 0, str(startAt)\n",
    "            for w in s:\n",
    "                if w != cur:\n",
    "                    print(cur, 'move ',w)\n",
    "                    res += moveCost\n",
    "                    cur = w\n",
    "                res += pushCost\n",
    "\n",
    "            print(res)\n",
    "            return res\n",
    "\n",
    "\n",
    "\n",
    "        n1, n2 = targetSeconds // 60, targetSeconds % 60\n",
    "\n",
    "        return min(resolve(n1, n2), resolve(n1 - 1, n2 + 60))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def calc(s: str) -> int:\n",
    "            s = s.lstrip('0')\n",
    "            cost = pushCost * len(s)\n",
    "            cur = startAt\n",
    "            for char in s:\n",
    "                if ord(char) - ord('0') != cur:\n",
    "                    cost += moveCost\n",
    "                    cur = ord(char) - ord('0')\n",
    "            return cost\n",
    "\n",
    "        res = inf\n",
    "\n",
    "        # seconds digit\n",
    "        if targetSeconds < 100: \n",
    "            res = min(res, calc(f\"{targetSeconds:02}\"))\n",
    "\n",
    "        # minutes + seconds\n",
    "        if 60 <= targetSeconds < 6000:\n",
    "            res = min(res, calc(f\"{targetSeconds // 60}{targetSeconds % 60:02}\"))\n",
    "\n",
    "        if targetSeconds >= 60 and targetSeconds % 60 < 40:\n",
    "            res = min(res, calc(f\"{targetSeconds // 60 - 1}{targetSeconds % 60 + 60:02}\"))\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def resolve(n1, n2):\n",
    "            if n1 < 0 or n1 > 99 or n2 > 99: return float('inf')\n",
    "            s = str(n2) if n1 == 0 else (str(n1) + (str(n2) if n2 > 9 else '0' + str(n2)))\n",
    "            res, cur = 0, str(startAt)\n",
    "            for w in s:\n",
    "                if w != cur:\n",
    "                    res += moveCost\n",
    "                    cur = w\n",
    "                res += pushCost\n",
    "            return res\n",
    "\n",
    "        n1, n2 = targetSeconds // 60, targetSeconds % 60\n",
    "        return min(resolve(n1, n2), resolve(n1 - 1, n2 + 60))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, sec: int) -> int:\n",
    "        def calc(s: str) -> int:\n",
    "            cost = pushCost * len(s)\n",
    "            cur = startAt\n",
    "            for ch in s:\n",
    "                if ord(ch) - ord('0') != cur:\n",
    "                    cost += moveCost\n",
    "                    cur = ord(ch) - ord('0')\n",
    "            return cost\n",
    "\n",
    "        ans = inf\n",
    "        if 60 <= sec < 6000: ans = calc(f\"{sec // 60}{sec % 60 :02}\")\n",
    "        if sec < 100: ans = min(ans, calc(str(sec)))  # 仅输入秒数\n",
    "        elif sec % 60 < 40: ans = min(ans, calc(f\"{sec // 60 - 1}{sec % 60 + 60}\"))  # 借一分钟给秒数\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        minu, sec = targetSeconds // 60, targetSeconds % 60\n",
    "        def getans(minu: int, sec: int) -> int:\n",
    "            if minu < 0 or minu >= 100 or sec >= 100:\n",
    "                return float(\"inf\")\n",
    "            ans, s = 0, startAt\n",
    "            st = str(minu * 100 + sec)\n",
    "            for c in st:\n",
    "                ans += pushCost if s == ord(c) - ord('0') else pushCost + moveCost\n",
    "                s = ord(c) - ord('0')\n",
    "            return ans\n",
    "        return min(getans(minu, sec), getans(minu - 1, sec + 60))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:\n",
    "        \n",
    "        def calc(s):\n",
    "            n = len(s)\n",
    "            ans = n * pushCost\n",
    "            cur = str(startAt)\n",
    "            for c in s:\n",
    "                if not c == cur:\n",
    "                    ans += moveCost\n",
    "                cur = c\n",
    "            return ans\n",
    "        \n",
    "        ans = inf\n",
    "\n",
    "        # 处理正常的分+秒的情况\n",
    "        if 60 <= t < 6000:\n",
    "            s = f\"{t // 60}{ str(t % 60).zfill(2)}\"\n",
    "            ans = calc(s)\n",
    "        if t < 100:\n",
    "            s = f\"{t}\"\n",
    "            ans = min(ans, calc(s))\n",
    "        # 保证可借且分不为0\n",
    "        if t >= 100 and t % 60 < 40:\n",
    "            s = f\"{t // 60 - 1}{t % 60 + 60}\"\n",
    "            ans = min(ans, calc(s))\n",
    "        \n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        \n",
    "        m, s = divmod(targetSeconds, 60)\n",
    "\n",
    "        def f(a):\n",
    "            ans = 0\n",
    "            pre = str(startAt)\n",
    "            for c in a:\n",
    "                if c == pre:\n",
    "                    ans += pushCost\n",
    "                else:\n",
    "                    ans += moveCost + pushCost\n",
    "                pre = c\n",
    "            return ans\n",
    "        a = str(m) + str(s).rjust(2, '0')\n",
    "        if m> 99:\n",
    "            a = str(m-1) + str(s+60).rjust(2, '0')\n",
    "        while a[0] == '0':\n",
    "            a = a[1:]\n",
    "        ans = f(a)\n",
    "        if m > 0 and s + 60 < 100:\n",
    "            a = str(m - 1) + str(s + 60).rjust(2, '0')\n",
    "            while a[0] =='0':\n",
    "                a = a[1:]\n",
    "            ans = min(ans, f(a))\n",
    "\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        #target的表达最多有两种，一种为最大化分钟，如果对60取余数后小于等于39，则存在第二种表达，最大化秒钟\n",
    "        #ta1 = (targetSeconds // 60) * 100 + targetSeconds % 60\n",
    "        #ta2 = ((targetSeconds // 60)-1) * 100 + targetSeconds % 60 +60\n",
    "        def cost(m : int,s : int) -> int:\n",
    "            if not(0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float(\"INF\")\n",
    "            digits =[m // 10,m % 10,s // 10,s % 10]\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] ==0:\n",
    "                start += 1\n",
    "            ans = 0\n",
    "            prev = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d !=prev:\n",
    "                    prev = d\n",
    "                    ans += moveCost\n",
    "                ans +=pushCost\n",
    "            return ans\n",
    "        mm,ss = targetSeconds // 60,targetSeconds % 60\n",
    "        return min(cost(mm,ss),cost(mm-1,ss+60))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \"\"\"startAt是指里面有只手悬在startAt位置上，move就是把这只手move到其他数字上面，push就是输入手所在的这个数字\"\"\"\n",
    "\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        mini = targetSeconds // 60\n",
    "        sec = targetSeconds % 60\n",
    "\n",
    "        first = mini // 10\n",
    "        second = mini % 10\n",
    "\n",
    "        third = sec // 10\n",
    "        forth = sec % 10\n",
    "\n",
    "        def help(current, *targets):\n",
    "\n",
    "            if not targets:\n",
    "                return 0\n",
    "\n",
    "            target, *nexts = targets\n",
    "            if current == target:\n",
    "                return pushCost + help(target, *nexts)\n",
    "            else:\n",
    "                return pushCost + moveCost + help(target, *nexts)\n",
    "\n",
    "        def inner(one, two, three, four):\n",
    "            if two < 0:\n",
    "                if not one:\n",
    "                    return inf\n",
    "\n",
    "                one -= 1\n",
    "                two += 10\n",
    "\n",
    "            if one > 9 or two > 9 or three > 9 or four > 9:\n",
    "                return inf\n",
    "\n",
    "            res = 0\n",
    "            if not one:\n",
    "                if not two:\n",
    "                    if not three:\n",
    "                        res += help(startAt, four)\n",
    "                    else:\n",
    "                        res += help(startAt, three, four)\n",
    "                else:\n",
    "                    res += help(startAt, two, three, four)\n",
    "            else:\n",
    "                res += help(startAt, one, two, three, four)\n",
    "\n",
    "            return res\n",
    "\n",
    "        normal = inner(first, second, third, forth)\n",
    "        carry = inner(first, second - 1, third + 6, forth)\n",
    "        return min(normal, carry)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:\n",
    "        def calc(s):\n",
    "            n = len(s)\n",
    "            cost = n * pushCost\n",
    "            cur = str(startAt)\n",
    "            for c in s:\n",
    "                if cur != c:\n",
    "                    cost += moveCost\n",
    "                    cur = c\n",
    "            print(s, cost)\n",
    "            return cost\n",
    "\n",
    "        ans = inf\n",
    "        # 分+秒的形式\n",
    "        if 60 <= t < 6000:\n",
    "            s = str(t // 60) + str(t % 60).zfill(2)\n",
    "            ans = min(ans, calc(s))\n",
    "        # 秒的形式：\n",
    "        if t < 100:\n",
    "            ans = min(ans, calc(str(t)))\n",
    "        # 分可以借给秒的情况\n",
    "        if t >=100 and t % 60 < 40:\n",
    "            s = str(t // 60 - 1) + str(t % 60 + 60)\n",
    "            ans = min(ans, calc(str(s)))\n",
    "        \n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def func(t):\n",
    "            ans = 0\n",
    "            pre = str(startAt)\n",
    "            for i in range(len(t)):\n",
    "                if t[i] == '0' and ans == 0:\n",
    "                    continue\n",
    "                ans += pushCost if t[i] == pre else moveCost + pushCost\n",
    "                pre = t[i]\n",
    "            return ans\n",
    "\n",
    "        mi, se = divmod(targetSeconds, 60)\n",
    "        if mi == 100:\n",
    "            return func(str(mi - 1) + str(se + 60))\n",
    "        if mi != 0 and se < 40:\n",
    "            a = func(str(mi - 1).zfill(2) + str(se + 60))\n",
    "            b = func(str(mi).zfill(2) + str(se).zfill(2))\n",
    "            return min(a, b)\n",
    "        return func(str(mi).zfill(2) + str(se).zfill(2))                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def calc(s):\n",
    "            cost = pushCost * len(s)\n",
    "            cur = str(startAt)\n",
    "            for ch in s:\n",
    "                if ch != cur:\n",
    "                    cost += moveCost\n",
    "                    cur = ch\n",
    "            return cost \n",
    " \n",
    "        ans = inf\n",
    "        if targetSeconds < 100:\n",
    "            ans = min(ans, calc(str(targetSeconds)))\n",
    "        elif targetSeconds % 60 < 40:\n",
    "            ans = min(ans, calc(f\"{targetSeconds // 60 - 1}{targetSeconds % 60 + 60}\"))\n",
    "        if 60 <= targetSeconds < 6000:\n",
    "            ans = min(ans, calc(f\"{targetSeconds // 60}{targetSeconds % 60 :02}\"))\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "\n",
    "        def cost(m: int, s: int) -> int:\n",
    "            if not (0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float(\"INF\")\n",
    "            digits = [m // 10, m % 10, s // 10, s % 10]\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] == 0:\n",
    "                start += 1\n",
    "\n",
    "            res = 0\n",
    "            prev = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d != prev:\n",
    "                    prev = d\n",
    "                    res += moveCost\n",
    "                res += pushCost\n",
    "            return res\n",
    "\n",
    "        mm, ss = targetSeconds // 60, targetSeconds % 60\n",
    "        return min(cost(mm, ss), cost(mm - 1, ss + 60))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def calcCost(time):\n",
    "            result = 0\n",
    "            pre = str(startAt)\n",
    "            for c in time:\n",
    "                if(pre != c):\n",
    "                    result += moveCost\n",
    "                    pre = c\n",
    "                result += pushCost\n",
    "            return result\n",
    "        \n",
    "        times = []\n",
    "        s = targetSeconds % 60\n",
    "        m = targetSeconds // 60\n",
    "        if(m == 0):\n",
    "            times.append(str(s))\n",
    "        else:\n",
    "            if(m <= 99):\n",
    "                if(s < 10):\n",
    "                    times.append(str(m)+'0'+str(s))\n",
    "                else:\n",
    "                    times.append(str(m)+str(s))\n",
    "\n",
    "            if(s + 60 <= 99):\n",
    "                if(m-1 == 0):\n",
    "                    times.append(str(s+60))\n",
    "                else:\n",
    "                    times.append(str(m-1)+str(s+60))\n",
    "            if(s >= 60):\n",
    "                if(s-60 < 10):\n",
    "                    times.append(str(m+1) + '0' + str(s-60))\n",
    "                else:\n",
    "                    times.append(str(m+1) + str(s-60))\n",
    "        result = float('inf')\n",
    "        for time in times:\n",
    "            result = min(result, calcCost(time))\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "\n",
    "        def cost(m: int, s: int) -> int:\n",
    "            if not (0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float(\"INF\")\n",
    "            digits = [m // 10, m % 10, s // 10, s % 10]\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] == 0:\n",
    "                start += 1\n",
    "\n",
    "            res = 0\n",
    "            prev = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d != prev:\n",
    "                    prev = d\n",
    "                    res += moveCost\n",
    "                res += pushCost\n",
    "            return res\n",
    "\n",
    "        mm, ss = targetSeconds // 60, targetSeconds % 60\n",
    "        return min(cost(mm, ss), cost(mm - 1, ss + 60))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, sec: int) -> int:\n",
    "        def calc(s: str) -> int:\n",
    "            cost = pushCost * len(s)\n",
    "            cur = startAt\n",
    "            for ch in s:\n",
    "                if ord(ch) - ord('0') != cur:\n",
    "                    cost += moveCost\n",
    "                    cur = ord(ch) - ord('0')\n",
    "            return cost\n",
    "\n",
    "        ans = inf\n",
    "        if 60 <= sec < 6000: ans = calc(f\"{sec // 60}{sec % 60 :02}\")\n",
    "        if sec < 100: ans = min(ans, calc(str(sec)))  # 仅输入秒数\n",
    "        elif sec % 60 < 40: ans = min(ans, calc(f\"{sec // 60 - 1}{sec % 60 + 60}\"))  # 借一分钟给秒数\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        M1 = targetSeconds//60\n",
    "        S1 = targetSeconds % 60\n",
    "        M2 = 0\n",
    "        S2 = 0\n",
    "        if M1 == 0 and S1 < 10:\n",
    "            return moveCost + pushCost if S1 != startAt else pushCost\n",
    "        if S1 <= 39:\n",
    "            M2 = M1 - 1\n",
    "            S2 = S1 + 60\n",
    "        # print(M1,S1,M2,S2)\n",
    "        res = inf\n",
    "        \n",
    "        temp = 0\n",
    "        pos = str(startAt)\n",
    "        if M1 < 100:\n",
    "            if M1 != 0:\n",
    "                M1 = str(M1)\n",
    "                for i in M1:\n",
    "                    if i != pos:\n",
    "                        temp += moveCost\n",
    "                    temp += pushCost\n",
    "                    pos = i\n",
    "            S1 = str(S1)\n",
    "            if len(S1) == 1:S1 = '0' + S1\n",
    "            for i in S1:\n",
    "                if i != pos:\n",
    "                    temp += moveCost\n",
    "                temp += pushCost\n",
    "                pos = i\n",
    "            res = min(res,temp)\n",
    "        if M2 or S2:\n",
    "            temp = 0\n",
    "            pos = str(startAt)\n",
    "            if M2 != 0:\n",
    "                M2 = str(M2)\n",
    "                for i in M2:\n",
    "                    if i != pos:\n",
    "                        temp += moveCost\n",
    "                    temp += pushCost\n",
    "                    pos = i\n",
    "            S2 = str(S2)\n",
    "            if len(S2) == 1:S2 = '0' + S2\n",
    "            for i in S2:\n",
    "                if i != pos:\n",
    "                    temp += moveCost\n",
    "                    pos = i\n",
    "                temp += pushCost\n",
    "            res = min(res,temp)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        res = inf\n",
    "\n",
    "        ans1 = (targetSeconds//60, targetSeconds%60)\n",
    "        ans2 = (ans1[0]-1, ans1[1] + 60)\n",
    "\n",
    "        def cal_cost(targetTime):\n",
    "            res = 0\n",
    "            targetTimeStr = (str(targetTime[0]) if targetTime[0] > 0 else \"\") + (str(targetTime[1]) if (targetTime[0] == 0 or targetTime[1] > 9) else \"0\" + str(targetTime[1]))\n",
    "            res += moveCost + pushCost if targetTimeStr[0] != str(startAt) else pushCost\n",
    "            for ind, ti in enumerate(targetTimeStr[1:]):\n",
    "                if ti != targetTimeStr[ind]:\n",
    "                    res += moveCost + pushCost\n",
    "                else:\n",
    "                    res += pushCost\n",
    "            return res\n",
    "\n",
    "        if ans1[0] <= 99:\n",
    "            res = min(res, cal_cost(ans1))\n",
    "\n",
    "        if ans2[0] >= 0 and ans2[1] <= 99:\n",
    "            res = min(res, cal_cost(ans2))\n",
    "\n",
    "        return res\n",
    "        \n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        res = inf\n",
    "\n",
    "        targetTime1 = (targetSeconds//60, targetSeconds%60)\n",
    "        targetTime2 = (targetTime1[0]-1, targetTime1[1] + 60)\n",
    "\n",
    "        def cal_cost(targetTime):\n",
    "            res = 0\n",
    "            targetTimeStr = \"{}{}{}\".format(startAt, \n",
    "                str(targetTime[0]) if targetTime[0] > 0 else \"\", \n",
    "                (str(targetTime[1]) if (targetTime[0] == 0 or targetTime[1] > 9) else \"0\" + str(targetTime[1])))\n",
    "            for ind, ti in enumerate(targetTimeStr[1:]):\n",
    "                if ti != targetTimeStr[ind]:\n",
    "                    res += moveCost + pushCost\n",
    "                else:\n",
    "                    res += pushCost\n",
    "            return res\n",
    "\n",
    "        if targetTime1[0] <= 99:\n",
    "            res = min(res, cal_cost(targetTime1))\n",
    "\n",
    "        if targetTime2[0] >= 0 and targetTime2[1] <= 99:\n",
    "            res = min(res, cal_cost(targetTime2))\n",
    "\n",
    "        return res\n",
    "        \n",
    "    \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:\n",
    "    #     def calc(s):\n",
    "    #         s = s.lstrip(\"0\")\n",
    "    #         n = len(s)\n",
    "    #         cost = n * pushCost\n",
    "    #         cur = str(startAt)\n",
    "    #         for c in s:\n",
    "    #             if cur != c:\n",
    "    #                 cost += moveCost\n",
    "    #                 cur = c\n",
    "    #         print(s, cost)\n",
    "    #         return cost\n",
    "\n",
    "    #     ans = inf\n",
    "    #     # 分+秒的形式\n",
    "    #     if 60 <= t <= 6000:\n",
    "    #         s = str(t // 60) + str(t % 60).zfill(2)\n",
    "    #         ans = min(ans, calc(s))\n",
    "    #     # 秒的形式：\n",
    "    #     if t < 100:\n",
    "    #         ans = min(ans, calc(str(t)))\n",
    "    #     # 分可以借给秒的情况\n",
    "    #     elif t > 60 and t % 60 < 40:\n",
    "    #         s = str(t // 60 - 1) + str(t % 60 + 60)\n",
    "    #         ans = min(ans, calc(str(s)))\n",
    "        \n",
    "    #     return ans\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, sec: int) -> int:\n",
    "            def calc(s: str) -> int:\n",
    "                cost = pushCost * len(s)\n",
    "                cur = startAt\n",
    "                for ch in s:\n",
    "                    if ord(ch) - ord('0') != cur:\n",
    "                        cost += moveCost\n",
    "                        cur = ord(ch) - ord('0')\n",
    "                return cost\n",
    "\n",
    "            ans = inf\n",
    "            if 60 <= sec < 6000: ans = calc(f\"{sec // 60}{sec % 60 :02}\")\n",
    "            if sec < 100: ans = min(ans, calc(str(sec)))  # 仅输入秒数\n",
    "            elif sec % 60 < 40: ans = min(ans, calc(f\"{sec // 60 - 1}{sec % 60 + 60}\"))  # 借一分钟给秒数\n",
    "            return ans\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:\n",
    "        def calc(s):\n",
    "            s = s.lstrip(\"0\")\n",
    "            n = len(s)\n",
    "            cost = n * pushCost\n",
    "            cur = str(startAt)\n",
    "            for c in s:\n",
    "                if cur != c:\n",
    "                    cost += moveCost\n",
    "                    cur = c\n",
    "            print(s, cost)\n",
    "            return cost\n",
    "\n",
    "        ans = inf\n",
    "        # 分+秒的形式\n",
    "        if 60 <= t < 6000:\n",
    "            s = str(t // 60) + str(t % 60).zfill(2)\n",
    "            ans = min(ans, calc(s))\n",
    "        # 秒的形式：\n",
    "        if t < 100:\n",
    "            ans = min(ans, calc(str(t)))\n",
    "        # 分可以借给秒的情况\n",
    "        if t >=100 and t % 60 < 40:\n",
    "            s = str(t // 60 - 1) + str(t % 60 + 60)\n",
    "            ans = min(ans, calc(str(s)))\n",
    "        \n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:\n",
    "        def calc(s):\n",
    "            s = s.lstrip(\"0\")\n",
    "            n = len(s)\n",
    "            cost = n * pushCost\n",
    "            cur = str(startAt)\n",
    "            for c in s:\n",
    "                if cur != c:\n",
    "                    cost += moveCost\n",
    "                    cur = c\n",
    "            print(s, cost)\n",
    "            return cost\n",
    "\n",
    "        ans = inf\n",
    "        # 分+秒的形式\n",
    "        if 60 <= t < 6000:\n",
    "            s = str(t // 60) + str(t % 60).zfill(2)\n",
    "            ans = min(ans, calc(s))\n",
    "        # 秒的形式：\n",
    "        if t < 100:\n",
    "            ans = min(ans, calc(str(t)))\n",
    "        # 分可以借给秒的情况\n",
    "        elif t > 60 and t % 60 < 40:\n",
    "            s = str(t // 60 - 1) + str(t % 60 + 60)\n",
    "            ans = min(ans, calc(str(s)))\n",
    "        \n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:\n",
    "        \n",
    "\n",
    "        def calc(s):\n",
    "            n = len(s)\n",
    "            cost = n * pushCost\n",
    "            curr = str(startAt)\n",
    "            for c in s:\n",
    "                if not c == curr:\n",
    "                    cost += moveCost\n",
    "                curr = c\n",
    "            return cost\n",
    "        \n",
    "        # 处理正常的分秒的情况\n",
    "        ans = inf\n",
    "        if 60 <= t < 6000:\n",
    "            s = f\"{t // 60}{str(t % 60).zfill(2)}\"\n",
    "            ans = min(ans, calc(s))\n",
    "        # 处理只有秒的情况\n",
    "        if t <= 99:\n",
    "            s = f\"{t}\"\n",
    "            ans = min(ans, calc(s))\n",
    "        # 处理可以借一位给秒，且存在分的情况\n",
    "        if t > 100 and t % 60 < 40:\n",
    "            s = f\"{t // 60 - 1}{t % 60 + 60}\"\n",
    "            ans = min(ans, calc(s))\n",
    "        \n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        ans=inf\n",
    "\n",
    "        minit,se=targetSeconds//60,targetSeconds%60\n",
    "        if minit>=100:\n",
    "            minit,se=targetSeconds//60-1,targetSeconds%60+60\n",
    "\n",
    "        l=[]\n",
    "        if minit!=0:\n",
    "            if minit>=10:\n",
    "                l.append(minit//10)\n",
    "                l.append(minit%10)\n",
    "            else:\n",
    "                l.append(minit)\n",
    "        \n",
    "        if se>=10:\n",
    "            l.append(se//10)\n",
    "            l.append(se%10)\n",
    "        else:\n",
    "            if l:\n",
    "                l.extend([0,se])\n",
    "            else:l.append(se)\n",
    "        l.insert(0,startAt)\n",
    "        temp=0\n",
    "        for i in range(1,len(l)):\n",
    "            if l[i]!=l[i-1]:\n",
    "                temp+=moveCost\n",
    "\n",
    "        ans=min(ans,temp+(len(l)-1)*pushCost)\n",
    "\n",
    "\n",
    "        minit,se=targetSeconds//60-1,targetSeconds%60+60\n",
    "        if minit==-1 or se>=100:\n",
    "            return ans\n",
    "\n",
    "        l=[]\n",
    "        if minit!=0:\n",
    "            if minit>=10:\n",
    "                l.append(minit//10)\n",
    "                l.append(minit%10)\n",
    "            else:\n",
    "                l.append(minit)\n",
    "        \n",
    "        if se>=10:\n",
    "            l.append(se//10)\n",
    "            l.append(se%10)\n",
    "        else:\n",
    "            if l:\n",
    "                l.extend([0,se])\n",
    "            else:l.append(se)\n",
    "\n",
    "        l.insert(0,startAt)\n",
    "        temp=0\n",
    "        for i in range(1,len(l)):\n",
    "            if l[i]!=l[i-1]:\n",
    "                temp+=moveCost\n",
    "\n",
    "        ans=min(ans,temp+(len(l)-1)*pushCost)\n",
    "\n",
    "\n",
    "\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def check(m,s):\n",
    "            if not(0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float('inf')\n",
    "            ind = [m//10,m%10,s//10,s%10]\n",
    "            start = 0\n",
    "            while start < 4 and ind[start] == 0:\n",
    "                start += 1\n",
    "            ans = 0\n",
    "            pre = startAt\n",
    "            for d in ind[start:]:\n",
    "                if d != pre:\n",
    "                    ans += moveCost\n",
    "                    pre = d\n",
    "                ans += pushCost\n",
    "            return ans\n",
    "        mm,ss = targetSeconds//60,targetSeconds%60\n",
    "        return min(check(mm,ss),check(mm-1,ss+60))\n",
    "\n",
    "\n",
    "        def cost(m,s):\n",
    "            if not (0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float('inf')\n",
    "            digits = [m//10,m%10,s//10,s%10]\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] == 0:\n",
    "                start += 1\n",
    "            res = 0\n",
    "            pre = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d != pre:\n",
    "                    res += moveCost\n",
    "                    pre = d\n",
    "                res += pushCost\n",
    "            return res\n",
    "        \n",
    "        mm,ss = targetSeconds//60,targetSeconds%60\n",
    "        return min(cost(mm,ss),cost(mm-1,ss+60))\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        # 如果target小于等于59秒，只有一种表示方法\n",
    "        # 如果target大于等于60秒,就有两种表示方法\n",
    "        # target//60,target%60\n",
    "        # target//60-1,target%60+60\n",
    "        def cost(m,s):\n",
    "            if not (0 <= m <= 99 and 0 <= s <= 99):\n",
    "                return float('inf')\n",
    "            digits = [m//10,m%10,s//10,s%10]\n",
    "            start = 0\n",
    "            while start < 4 and digits[start] == 0:\n",
    "                start += 1\n",
    "            res = 0\n",
    "            pre = startAt\n",
    "            for d in digits[start:]:\n",
    "                if d != pre:\n",
    "                    res += moveCost\n",
    "                    pre = d\n",
    "                res += pushCost\n",
    "            return res\n",
    "        \n",
    "        mm,ss = targetSeconds//60,targetSeconds%60\n",
    "        return min(cost(mm,ss),cost(mm-1,ss+60))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, t: int) -> int:\n",
    "        def calc(s):\n",
    "            n = len(s)\n",
    "            cost = n * pushCost\n",
    "            cur = str(startAt)\n",
    "            for c in s:\n",
    "                if cur != c:\n",
    "                    cost += moveCost\n",
    "                    cur = c\n",
    "            print(s, cost)\n",
    "            return cost\n",
    "\n",
    "        ans = inf\n",
    "        # 分+秒的形式\n",
    "        if 60 <= t < 6000:\n",
    "            s = str(t // 60) + str(t % 60).zfill(2)\n",
    "            ans = min(ans, calc(s))\n",
    "        # 秒的形式：\n",
    "        if t < 100:\n",
    "            ans = min(ans, calc(str(t)))\n",
    "        # 分可以借给秒的情况\n",
    "        if t >=100 and t % 60 < 40:\n",
    "            s = str(t // 60 - 1) + str(t % 60 + 60)\n",
    "            ans = min(ans, calc(str(s)))\n",
    "        \n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def minCostSetTime(self, startAt: int, moveCost: int, pushCost: int, targetSeconds: int) -> int:\n",
    "        def calc(s: str) -> int:\n",
    "            s = s.lstrip('0')   # no need to push prefixed 0\n",
    "            cost = pushCost * len(s)\n",
    "            cur = startAt\n",
    "            for char in s:\n",
    "                if ord(char) - ord('0') != cur:\n",
    "                    cost += moveCost\n",
    "                    cur = ord(char) - ord('0')\n",
    "            return cost\n",
    "\n",
    "        res = inf\n",
    "\n",
    "        # seconds digit\n",
    "        if targetSeconds < 60: \n",
    "            res = min(res, calc(f\"{targetSeconds:02}\"))\n",
    "\n",
    "        # minutes + seconds\n",
    "        if 60 <= targetSeconds < 6000:\n",
    "            res = min(res, calc(f\"{targetSeconds // 60}{targetSeconds % 60:02}\"))\n",
    "\n",
    "        # grant one minute to seconds\n",
    "        if targetSeconds >= 60 and targetSeconds % 60 < 40:\n",
    "            res = min(res, calc(f\"{targetSeconds // 60 - 1}{targetSeconds % 60 + 60:02}\"))\n",
    "\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
